/*
 * @(#)MaintDmsRootAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsLocMaster;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.bean.DmsValidation;
import com.dcivision.dms.core.DocumentOperationManager;
import com.dcivision.dms.core.DocumentRetrievalManager;
import com.dcivision.dms.core.DocumentValidateManager;
import com.dcivision.dms.core.RootOperationManager;
import com.dcivision.dms.core.RootRetrievalManager;
import com.dcivision.dms.dao.DmsLocMasterDAObject;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.framework.ApplicationContainer;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractActionForm;
/**
  MaintDmsRootAction.java

  This class is for maint dms root.

    @author          Rollo Chan
    @company         DCIVision Limited
    @creation date   13/08/2003
    @version         $Revision: 1.25.4.1 $
*/
/* ********************************Modify History***********************************************
 * Modify by  : Jicle.Wang 
 * Modify date: 2006/08/17
 * BugID      : 6107
 * Description: It should not prompt 'Record deleted successfully' if delete is failure.
 * *********************************************************************************************
 */
public class MaintDmsRootAction extends com.dcivision.setup.web.AbstractMaintPermissionAction {

  public static final String REVISION = "$Revision: 1.25.4.1 $";

  public MaintDmsRootAction() {
    super();
    this.objectIDProperty = "rootFolderID";
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.dms.dao.DmsRootDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.DMS_ADMIN);
  }

  /**
   * clearUniqueFields
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void clearUniqueFields(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    MaintDmsRootForm dmsRootForm = (MaintDmsRootForm)form;
    dmsRootForm.setRootName(null);
  }



  /**
   * selectRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */

  public void selectRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.selectRecord(mapping,form,request,response);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    Connection conn = this.getConnection(request);

    DmsRootDAObject dmsRootDAO = (DmsRootDAObject)this.getMajorDAObject(request);
    DmsLocMasterDAObject dmsLocDAO= new DmsLocMasterDAObject(sessionContainer,conn);
    MaintDmsRootForm dmsRootForm = (MaintDmsRootForm)form;
    DmsRoot dmsRoot=(DmsRoot)dmsRootDAO.getObjectByID(TextUtility.parseIntegerObj(form.getID()));
    if (dmsRoot!=null){
    	DmsLocMaster dmsDoc=(DmsLocMaster)dmsLocDAO.getObjectByID(dmsRoot.getLocID());
     if(dmsRoot.getStorageLimit()!=null){
         if (dmsRoot.getStorageLimit().doubleValue()==0.0){
            dmsRoot.setUnlimit("Y");
         }
     }
    }
    dmsRootForm.setUnlimit(dmsRoot.getUnlimit());
    dmsRootForm.setFromWhom(dmsRoot.getOwnerID().toString());
    dmsRootDAO=null;
    dmsLocDAO=null;
    sessionContainer = null;
    conn = null;
  }



  /**
   * insertRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void insertRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    Connection conn = this.getConnection(request);
    RootOperationManager rootOperationManager = new RootOperationManager(this.getSessionContainer(request), conn);
    DmsRoot dmsRoot = (DmsRoot)form.getFormData();
    MaintDmsRootForm dmsRootForm = (MaintDmsRootForm)form;
    // check for invalid characters on document name only
    TextUtility.stringValidation(dmsRoot.getRootName());
    dmsRoot.setOwnerID(TextUtility.parseIntegerObj(dmsRootForm.getFromWhom()));
    dmsRoot.setLocID(TextUtility.parseIntegerObj(dmsRootForm.getLocPath()));
    dmsRoot = rootOperationManager.createPublicRoot(dmsRoot);
    form.setFormData(dmsRoot);
    this.updatePermissionObjects(mapping, form, request, response);
    try {rootOperationManager.release();} catch (Exception e) { log.error(e, e);} finally { conn = null; }
  }

  /**
   * updateRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void updateRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    Connection conn = this.getConnection(request);
    RootOperationManager rootManager = new RootOperationManager(this.getSessionContainer(request), conn);
    DmsRoot dmsRoot = (DmsRoot)form.getFormData();
    MaintDmsRootForm dmsRootForm = (MaintDmsRootForm)form;
    if(Utility.isEmpty(dmsRootForm.getStorageLimit())){
      dmsRoot.setStorageLimit(new Double(0));
    }
    // check for invalid characters on document name only
    TextUtility.stringValidation(dmsRoot.getRootName());
    dmsRoot.setOwnerID(TextUtility.parseIntegerObj(dmsRootForm.getFromWhom()));
    dmsRoot = rootManager.updatePublicRoot(dmsRoot);
    form.setFormData(dmsRoot);
    this.updatePermissionObjects(mapping, form, request, response);
    try {rootManager.release();} catch (Exception e) { log.error(e, e);} finally { conn = null; }
  }

  /**
   * copyRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void copyRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    super.copyRecord(mapping, form, request, response);
    MaintDmsRootForm rootForm = (MaintDmsRootForm)form;
    Connection conn = this.getConnection(request);
    RootOperationManager rootOperationManager = new RootOperationManager(this.getSessionContainer(request), conn);
//    rootOperationManager
    /************************************************************************
     * TODO: Copy Folder Structure or even all Files depends on CopyMode.
     ************************************************************************/
    try {rootOperationManager.release();} catch (Exception e) { log.error(e, e);} finally { conn = null; }

  }

  /**
   * deleteRecord
   *
   * @param mapping
   * @param form
   * @param request
   * @param response
   * @throws ApplicationException
   */
  public void deleteRecordOld(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    Connection conn = this.getConnection(request);
    RootOperationManager rootManager = new RootOperationManager(this.getSessionContainer(request), conn);
    DmsRoot dmsRoot = (DmsRoot)form.getFormData();
    dmsRoot = rootManager.deletePublicRoot(dmsRoot.getID());
    form.setFormData(dmsRoot);
    this.updatePermissionObjects(mapping, form, request, response);
    try {rootManager.release();} catch (Exception e) {log.error(e, e);} finally { conn = null; }

  }
  
  /**
   * @deprecated This method is deprecated
   */
  public void deleteRecord(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    ApplicationContainer container = (ApplicationContainer)request.getSession().getServletContext().getAttribute(GlobalConstant.APPLICATION_CONTAINER_KEY);
    
    DmsRoot dmsRoot = (DmsRoot)form.getFormData();
    Integer rootId = dmsRoot.getID();
    DmsDocument sourceDocument = rootRetrievalManager.getRootDocument(rootId);
    boolean isIgnoreDeleteShortcut = ((MaintDmsRootForm)form).isIgnoreDeleteShortcut();
    DmsValidation validation = docValidateManager.validateDeleteAction(sourceDocument, isIgnoreDeleteShortcut);
    
    //step 2: begin delete documents
    String cacheKey = "D"+sessionContainer.getSessionID()+""+System.currentTimeMillis();
    try {
      List deleteDocumentIds = validation.getLstIdAccept();
      //step 2.1: check and lock operation id to application cache
      boolean success = container.checkAndLockDocumentOperationID(cacheKey, deleteDocumentIds, "ACDIMU");
      if (!success) {
        this.addError(request, "errors.dms.fail_to_delete", docRetrievalManager.getLocationPath(sourceDocument.getID()));
        this.addError(request, "errors.dms.cannot_edit_now");
      }else {
        //step 2.2: delete documents
        docOperationManager.deleteDocument(sourceDocument, validation);
        
        //step 2.3: message throws
        List lstIdHasRight = validation.getLstIdAccept();
        List lstIdMisRight = validation.getLstIdReject();
        List lstIdBeLocked = validation.getLstIdlocked();
        List lstIdBeDeleted = validation.getLstIdDeleted();
        List lstIdBeArchived = validation.getLstIdArchived();
        List lstIdRelatedShortcut = validation.getLstIdHaveRelationship();
        String locationPath = docRetrievalManager.getLocationPath(sourceDocument.getID());
        if (validation.isSuccess()) {
          this.addMessage(request, "dms.message.document_deleted_success", locationPath, String.valueOf(lstIdHasRight.size()));
        }else {
          this.addError(request, "errors.dms.fail_to_delete", locationPath);
          for (int i=0; i<lstIdMisRight.size(); i++) {
            this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));                            
          }
          for (int i=0; i<lstIdBeLocked.size(); i++) {
            this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));                            
          }
          for (int i=0; i<lstIdBeDeleted.size(); i++) {
            this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));                            
          }
          for (int i=0; i<lstIdBeArchived.size(); i++) {
            this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));                            
          }
          for (int i=0; i<lstIdRelatedShortcut.size(); i++) {
            this.addError(request,"errors.dms.has_relationship",docRetrievalManager.getLocationPath((Integer)lstIdRelatedShortcut.get(i)));                            
          }
        }
        
        //step 2.4: delete from clipboard if it present

        List clipboardList = (List)request.getSession().getAttribute("DMS_CLIPBOARD");
        if (clipboardList!=null && !clipboardList.isEmpty()) {
          for (int i=0; i<clipboardList.size(); i++) {
            if(deleteDocumentIds.contains((Integer)clipboardList.get(i))) {
              clipboardList.remove(i--);
            }
          }
        }
      }
    }catch (ApplicationException ex) {
      throw ex;
    }catch (Exception ex) {
      throw new ApplicationException(ex);
    }finally {
      container.unlockDmsDocumentOperationID(cacheKey);
    }
    conn = null;
    sessionContainer = null;
    container = null;
    rootRetrievalManager.release();
    docValidateManager.release();
    docOperationManager.release();
    docRetrievalManager.release();
  }

  /**
   * getObjectType
   *
   * @return  The corresponding object type of permission setup action.
   */
  public String getObjectType() {
    return(GlobalConstant.OBJECT_TYPE_DOCUMENT);
  }
  /* Jicle.Wang 6107 2006/08/17 Start */
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    AbstractActionForm maintForm = (AbstractActionForm)form;

    String opMode = (maintForm!=null) ? maintForm.getOpMode() : request.getParameter("opMode");    //maintForm.getOpMode();
    String navMode = (maintForm!=null) ? maintForm.getNavMode() : request.getParameter("navMode"); //maintForm.getNavMode();
    log.debug( ((maintForm!=null) ? maintForm.getClass().getName() : "")+", OP Mode:"+opMode+", Nav Mode:"+navMode);

    // Set the function code for current action.
    ActionForward forward = this.retrieveFunctionCode(request, response, mapping);
    if (forward!=null) {
      return forward;
    }

    try {
      if (!Utility.isEmpty(opMode)) {
        if (GlobalConstant.OP_MODE_INSERT.equals(opMode)) {
          // Insert operation.
          try {
            this.insertRecord(mapping, maintForm, request, response);
            this.handleWorkflowRoutine(mapping, maintForm, request, response, opMode, navMode);
            this.commit(request);
            addMessage(request, MESSAGE_RECORD_INSERT);
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
          } else {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else if (GlobalConstant.OP_MODE_INSERT_NEW.equals(opMode)) {
          // Insert and New operation.
          try {
            this.insertRecord(mapping, maintForm, request, response);
            this.handleWorkflowRoutine(mapping, maintForm, request, response, opMode, navMode);
            this.commit(request);
            form.reset(mapping, request);
            this.init(mapping, maintForm, request, response);
            addMessage(request, MESSAGE_RECORD_INSERT);
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
          } else {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
          }
        } else if (GlobalConstant.OP_MODE_UPDATE.equals(opMode)) {
          // Update operation.
          try {
            this.updateRecord(mapping, maintForm, request, response);
            this.handleWorkflowRoutine(mapping, maintForm, request, response, opMode, navMode);
            this.commit(request);
            addMessage(request, MESSAGE_RECORD_UPDATE);
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
            return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
          } else {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else if (GlobalConstant.OP_MODE_UPDATE_NEW.equals(opMode)) {
          // Update and New operation.
          try {
            this.updateRecord(mapping, maintForm, request, response);
            this.handleWorkflowRoutine(mapping, maintForm, request, response, opMode, navMode);
            this.commit(request);
            form.reset(mapping, request);
            this.init(mapping, maintForm, request, response);
            addMessage(request, MESSAGE_RECORD_UPDATE);
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
            return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
          } else {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
          }
        } else if (GlobalConstant.OP_MODE_DELETE.equals(opMode)) {
          // Delete operation.
          try {
            boolean b = this.deleteRecordSuccessful(mapping, maintForm, request, response);
            this.handleWorkflowRoutine(mapping, maintForm, request, response, opMode, navMode);
            this.commit(request);
            if(b){
              addMessage(request, MESSAGE_RECORD_DELETE);
            }
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
            return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
          } else {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else if (GlobalConstant.OP_MODE_COPY.equals(opMode)) {
          // Copy operation.
          try {
            this.copyRecord(mapping, maintForm, request, response);
            this.handleWorkflowRoutine(mapping, maintForm, request, response, opMode, navMode);
            this.commit(request);
            addMessage(request, MESSAGE_RECORD_COPY);
          } catch (ApplicationException appEx) {
            this.rollback(request);
            handleApplicationException(request, appEx);
          }
          if (isError(request)) {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_CHANGE);
            return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
          } else {
            maintForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
            return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
          }
        } else {
          // Unknown operation.
          throw new ApplicationException(ErrorConstant.COMMON_UNKNOWN_OPERATION);
        }
      } else {
        if (Utility.isEmpty(navMode) || GlobalConstant.NAV_MODE_NEW.equals(navMode)) {
          // New navigation.
          this.init(mapping, maintForm, request, response);
          maintForm.setNavMode(GlobalConstant.NAV_MODE_NEW);
          return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
        } else if (GlobalConstant.NAV_MODE_EDIT.equals(navMode)) {
          // Edit navigation.
          try {
            this.selectRecord(mapping, maintForm, request, response);
          } catch (ApplicationException appEx) {
            handleApplicationException(request, appEx);
          }
          maintForm.setNavMode(GlobalConstant.NAV_MODE_EDIT);
          return mapping.findForward(GlobalConstant.NAV_MODE_EDIT);
        } else if (GlobalConstant.NAV_MODE_VIEW.equals(navMode)) {
          // View navigation.
          try {
            this.selectRecord(mapping, maintForm, request, response);
          } catch (ApplicationException appEx) {
            handleApplicationException(request, appEx);
          }
          maintForm.setNavMode(GlobalConstant.NAV_MODE_VIEW);
          return mapping.findForward(GlobalConstant.NAV_MODE_VIEW);
        } else if (GlobalConstant.NAV_MODE_CHANGE.equals(navMode)) {
          // Change navigation.
          try {
            this.selectRecord(mapping, maintForm, request, response);
          } catch (ApplicationException appEx) {
            handleApplicationException(request, appEx);
          }
          maintForm.setNavMode(GlobalConstant.NAV_MODE_CHANGE);
          maintForm.setID(null);
          maintForm.setCreateDate(null);
          maintForm.setCreatorID(null);
          maintForm.setUpdateDate(null);
          maintForm.setUpdaterID(null);
          maintForm.setUpdateCount(null);
          maintForm.setRecordStatus(null);
          clearUniqueFields(mapping, maintForm, request, response);
          return mapping.findForward(GlobalConstant.NAV_MODE_NEW);
        } else if (GlobalConstant.NAV_MODE_LIST.equals(navMode)) {
          // List navigation.
          maintForm.setNavMode(GlobalConstant.NAV_MODE_LIST);
          return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
        } else {
          // Unknown navigation.
          throw new ApplicationException(ErrorConstant.COMMON_UNKNOWN_NAVIGATION);
        }
      }
    } catch (ApplicationException ex) {
      handleApplicationException(request, ex);
      return mapping.findForward(GlobalConstant.NAV_MODE_LIST);
    }
  }
  public boolean deleteRecordSuccessful(ActionMapping mapping, AbstractActionForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    Connection conn = this.getConnection(request);
    SessionContainer sessionContainer = this.getSessionContainer(request);
    DocumentValidateManager docValidateManager = new DocumentValidateManager(sessionContainer, conn);
    DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer, conn);
    DocumentRetrievalManager docRetrievalManager = new DocumentRetrievalManager(sessionContainer, conn);
    RootRetrievalManager rootRetrievalManager = new RootRetrievalManager(sessionContainer, conn);
    ApplicationContainer container = (ApplicationContainer)request.getSession().getServletContext().getAttribute(GlobalConstant.APPLICATION_CONTAINER_KEY);
    
    DmsRoot dmsRoot = (DmsRoot)form.getFormData();
    Integer rootId = dmsRoot.getID();
    DmsDocument sourceDocument = rootRetrievalManager.getRootDocument(rootId);
    boolean isIgnoreDeleteShortcut = ((MaintDmsRootForm)form).isIgnoreDeleteShortcut();
    DmsValidation validation = docValidateManager.validateDeleteAction(sourceDocument, isIgnoreDeleteShortcut);
    boolean b = false;
    
    //step 2: begin delete documents
    String cacheKey = "D"+sessionContainer.getSessionID()+""+System.currentTimeMillis();
    try {
      List deleteDocumentIds = validation.getLstIdAccept();
      //step 2.1: check and lock operation id to application cache
      boolean success = container.checkAndLockDocumentOperationID(cacheKey, deleteDocumentIds, "ACDIMU");
      if (!success) {
        this.addError(request, "errors.dms.fail_to_delete", docRetrievalManager.getLocationPath(sourceDocument.getID()));
        this.addError(request, "errors.dms.cannot_edit_now");
      }else {
        //step 2.2: delete documents
        docOperationManager.deleteDocument(sourceDocument, validation);
        
        //step 2.3: message throws
        List lstIdHasRight = validation.getLstIdAccept(); 
        List lstIdMisRight = validation.getLstIdReject();
        List lstIdBeLocked = validation.getLstIdlocked();
        List lstIdBeDeleted = validation.getLstIdDeleted();
        List lstIdBeArchived = validation.getLstIdArchived();
        List lstIdRelatedShortcut = validation.getLstIdHaveRelationship();
        String locationPath = docRetrievalManager.getLocationPath(sourceDocument.getID());
        if (validation.isSuccess()) {
        	b = true;
          this.addMessage(request, "dms.message.document_deleted_success", locationPath, String.valueOf(lstIdHasRight.size()));
        }else {
          this.addError(request, "errors.dms.fail_to_delete", locationPath);
          for (int i=0; i<lstIdMisRight.size(); i++) {
            this.addError(request,"errors.dms.no_permission",docRetrievalManager.getLocationPath((Integer)lstIdMisRight.get(i)));                            
          }
          for (int i=0; i<lstIdBeLocked.size(); i++) {
            this.addError(request,"errors.dms.been_checkout",docRetrievalManager.getLocationPath((Integer)lstIdBeLocked.get(i)));                            
          }
          for (int i=0; i<lstIdBeDeleted.size(); i++) {
            this.addError(request,"errors.dms.been_deleted",docRetrievalManager.getLocationPath((Integer)lstIdBeDeleted.get(i)));                            
          }
          for (int i=0; i<lstIdBeArchived.size(); i++) {
            this.addError(request,"errors.dms.been_archived",docRetrievalManager.getLocationPath((Integer)lstIdBeArchived.get(i)));                            
          }
          for (int i=0; i<lstIdRelatedShortcut.size(); i++) {
            this.addError(request,"errors.dms.has_relationship",docRetrievalManager.getLocationPath((Integer)lstIdRelatedShortcut.get(i)));                            
          }
          if (!Utility.isEmpty(lstIdHasRight)) {
            this.addError(request, "dms.message.document_been_deleted",String.valueOf(lstIdHasRight.size()));
          }
        }
        
        //step 2.4: delete from clipboard if it present

        List clipboardList = (List)request.getSession().getAttribute("DMS_CLIPBOARD");
        if (clipboardList!=null && !clipboardList.isEmpty()) {
          for (int i=0; i<clipboardList.size(); i++) {
            if(deleteDocumentIds.contains((Integer)clipboardList.get(i))) {
              clipboardList.remove(i--);
            }
          }
        }
      }
    }catch (ApplicationException ex) {
      throw ex;
    }catch (Exception ex) {
      throw new ApplicationException(ex);
    }finally {
      container.unlockDmsDocumentOperationID(cacheKey);
    }
    conn = null;
    sessionContainer = null;
    container = null;
    rootRetrievalManager.release();
    docValidateManager.release();
    docOperationManager.release();
    docRetrievalManager.release();
    return b;
  }
  /* Jicle.Wang 6107 2006/08/17 End   */
}
